;Copyright (C) 2010 Salil Bhagurkar

;This file is part of k3

;k3 is free software: you can redistribute it and/or modify
;it under the terms of the GNU Lesser General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.

;k3 is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU Lesser General Public License for more details.

;You should have received a copy of the GNU Lesser General Public License
;along with k3.  If not, see <http://www.gnu.org/licenses/>.

[bits 32]
[segment .text]
;Task management code:
extern _debug

extern _arch_end_of_task

toeip dd 0

_arch_resume_task:
	mov esp,[esp+4]
	pop dword[toeip]
	popf
	pop ebp
	pop edi
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	jmp dword[toeip]
ret ;)



_arch_start_task:
	mov esp,[esp+4]
	pop eax  ;This is the entry address of the function
	sti  ;Make sure interrupts are enabled
	call eax  ;Call the task
	jmp _arch_end_of_task
	jmp $
ret


global _arch_switch_task;(u32 stack,u32 *srcstack)
_arch_switch_task:
	push eax
	push ebx
	push ecx
	push edx
	push esi
	push edi
	push ebp
	pushf
	push .resume_point
	mov eax,[esp+44] ;Get the source stack pointer
	mov [eax],esp  ;Save current esp to the stack pointer location
	mov esp,[esp+40]
	pop dword[toeip]
	popf
	pop ebp
	pop edi
	pop esi
	pop edx
	pop ecx
	pop ebx
	pop eax
	jmp dword[toeip]
	.resume_point:
	mov eax,0
ret


msg db "Stack is %x", 10, 0

global _arch_switch_task_first;(u32 stack,u32 *srcstack)
_arch_switch_task_first:
	push eax
	push ebx
	push ecx
	push edx
	push esi
	push edi
	push ebp
	pushf
	push .resume_point
	mov eax,[esp+44] ;Get the source stack pointer
	mov [eax],esp ;Save current esp to the stack pointer location
	mov esp,[esp+40]
	pop eax  ;This is the entry address of the function
	sti  ;Make sure interrupts are enabled
	call eax  ;Call the task
	call _arch_end_of_task
	jmp $
	.resume_point:
ret
